import vue from "vue"
import vuex from 'vuex'
vue.use(vuex)
import $config from '@/api/http.baseUrl';
console.log('$config: ', $config);
const store = new vuex.Store({
    state: {
        socketTast: null,
        client_id: "",
        websocketData: {}, // 存放从后端接收到的websocket数据
        isclose: false,
    },
    mutations: {
        setWebsocketData(state, data) {
            state.websocketData = data
        },
        voice(state, data) {
            console.log('data: ', data);
            if (data.is_order == 1) {

                console.log(data.is_play == 1 ? '您有新的订单，请及时处理' : '你有订单已被用户取消')
                // console.log('data.is_play', data.is_play, 'data.is_cancel', data.is_cancel)
                // let content = data.is_play == 1 ? '您有新的订单，请及时处理' : '你有订单已被用户取消'
                let playMp3 = '/static/mp3/newOrder.mp3'
                let innerAudioContext = null
                innerAudioContext = uni.createInnerAudioContext();
                innerAudioContext.autoplay = true;
                innerAudioContext.src = playMp3;
                innerAudioContext.onPlay(() => {
                    console.log('开始播放');
                });
                innerAudioContext.onError((res) => {
                    console.log('res: ', res);
                    // console.log(res.errMsg);
                    // console.log(res.errCode);
                });
                innerAudioContext.onEnded((res) => {
                    // console.log('音频自然播放结束')
                    innerAudioContext.destroy()
                });
            }
        }
    },
    actions: {
        websocketInit({
            state,
            dispatch
        }) {
            if (!uni.getStorageSync('token')) return
            if (this.isclose) uni.closeSocket()
            console.log('websocketUrl', $config.websocketUrl);return;//20210326中断
            state.socketTast = uni.connectSocket({
                url: $config.websocketUrl,
                success: (e) => {
                    state.isclose = true;
                    console.log('websocket连接成功！')
                },
                fail: e => {
                    console.log('连接失败' + e)
                }
            })
            state.socketTast.onOpen(() => dispatch('websocketOnOpen'))
            state.socketTast.onMessage(res => dispatch('websocketOnMessage', res))
            state.socketTast.onClose(e => dispatch('websocketOnClose'))
            state.socketTast.onError(e => dispatch('websocketOnError'))
        },
        websocketOnOpen({
            commit
        }) {
            console.log('WebSocket连接正常打开中...！')
        },
        // 收到数据
        websocketOnMessage({
            state,
            commit,
            dispatch
        }, res) {
            let data = JSON.parse(res.data)
            console.log('接收Socket数据', data)
            switch (data.type) {
                case 'connect':
                    console.log('client_id', data.client_id)
                    state.client_id = data.client_id
                    uni.setStorageSync('client_id', data.client_id)
                    dispatch('bind_client_id', data.client_id)
                    break;
                case 'ping':
                    uni.sendSocketMessage({
                        data: '{type:"pong"}',
                    })
                    break;
                case 'house':
                    uni.$emit('orderType', data.data)
                    commit('voice', data.data)

                    // if (data.data.is_cancel == 1 || data.data.is_cancel == 0) {
                    //     commit('voice', data.data)
                    // }
                    break;
                default:
                    break;
            }

            if (res.data !== '连接成功') {
                commit('setWebsocketData', (res && JSON.parse(res.data) || null))
            }
        },
        bind_client_id({ state, commit, }, client_id) {
            uni.$u.api.bind_uid({ client_id }).then(res => {
                console.log('res: ', res);

            })
        },
        websocketOnClose({
            state,
            commit,
            dispatch
        }) {
            state.isclose = false;
            console.log('WebSocket连接关闭,重新连接！！！')
            // dispatch('websocketInit', state)
        },
        websocketOnError({
            state,
            commit,
            dispatch
        }) {
            // dispatch('websocketClose', state)
        },
        websocketClose({
            state
        }) {
            if (!state.socketTast) return
            state.socketTast.close({
                success(res) {
                    state.isclose = false;
                    state.socketTast = null
                    console.log('关闭成功', res)
                },
                fail(err) {
                    state.socketTast = null
                    console.log('关闭失败', err)
                }
            })
        },
        // 发送数据
        websocketSend({
            state,
            dispatch
        }, data) {
            uni.sendSocketMessage({
                data,
                success: res => {
                    console.log('发送成功', res)
                },
                fail: e => {
                    console.log('发送失败', e)
                    // uni.$emit('reconnection', '1')
                    state.socketTast = null
                    dispatch('websocketInit', state)
                }
            })
        }
    }
})
export default store